package richbar.com.richbar.fragment;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.content.ContextCompat;
import android.support.v4.view.ViewPager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.common.base.BaseActivity;
import com.common.base.BaseFragment;
import com.common.help.RecycleViewHelp;
import com.common.util.StringUtil;
import com.common.util.ToastUtil;
import com.okhttp.GlideUtil;
import com.okhttp.network.HttpUtils;
import com.okhttp.network.MyGson;
import java.util.ArrayList;
import java.util.List;
import richbar.com.richbar.R;
import richbar.com.richbar.activity.product.ProductListAc;
import richbar.com.richbar.adapter.home.GridAdapter1;
import richbar.com.richbar.adapter.home.HomeLeftRightAdapter;
import richbar.com.richbar.adapter.home.HomeRecommendAdapter;
import richbar.com.richbar.bean.GoodsBean;
import richbar.com.richbar.bean.HomePageBean;
import richbar.com.richbar.util.LoadingDialog;
import richbar.com.richbar.util.NetWorkUtil;
import richbar.com.richbar.util.Util;
import richbar.com.richbar.view.BannerLayout;

/**
 * Created by Administrator on 2018/10/13.
 * 首页
 */

public class HomePageFragment extends BaseFragment {
    private RecyclerView recommend_recyclerView;
    private HomeRecommendAdapter homeRecommendAdapter;
    private BannerLayout bannerLayout;
    private RecyclerView recycleView_fine_quality;
    private RecyclerView recycleView_promotion;
    private RecyclerView recyclerView_nav;
    private HomeLeftRightAdapter adapter1;
    private HomeLeftRightAdapter adapter2;
    private ViewPager viewPager;
    private LoadingDialog dialog;
    private List<HomePageBean.Data.Banner> list_banner = new ArrayList<>();//顶部轮播数据
    private List<HomePageBean.Data.Navlist> list_nav = new ArrayList<>();//导航栏数据
    private List<GoodsBean> list_HotRecommend = new ArrayList<>();//精品推荐
    private List<GoodsBean> list_Promotion = new ArrayList<>();//促销产品
    private List<GoodsBean> list_Recommend = new ArrayList<>();//推荐商品
    private GridAdapter1 adapter;
    private RecyclerView recyclerView1;//广告
    private RecyclerView recyclerView2;//广告
    private ImageView advert_iv1;
    private ImageView advert_iv2;
    private LinearLayout input_body;
    private TextView input_et;
    @Override
    protected View onCreate(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_homepage, container, false);
        recommend_recyclerView = view.findViewById(R.id.recycleView);
        dialog = new LoadingDialog(getActivity(), 0);
        input_body = view.findViewById(R.id.input_body);
        input_et = view.findViewById(R.id.input_et);
        input_et.setText("电烤鸡胸肉片");
        input_body.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                start(ProductListAc.class, new BaseActivity.OnBundleListener() {
                    @Override
                    public Bundle onSetBundle(Bundle bundle) {
                        bundle.putString("goodsName", StringUtil.getStr(input_et));
                        return bundle;
                    }
                });
            }
        });
        return view;
    }

    @Override
    protected void initData() {
        RecycleViewHelp.setGridMannager(recommend_recyclerView, 0, 2, getActivity());
        homeRecommendAdapter = new HomeRecommendAdapter(R.layout.item_home_page, new ArrayList<GoodsBean>());
        recommend_recyclerView.setAdapter(homeRecommendAdapter);
        getTopView();
    }

    /**
     * 获取网路数据
     */
    private void getNetData(final View topView) {
        if (!Util.checkNet(getActivity())) {
            ToastUtil.showToast(getActivity(), getString(R.string.error_net));
        } else {
            dialog.show();
            NetWorkUtil.getHomeData(Util.md5(), new HttpUtils.HttpCallback() {
                @Override
                public void onSuccess(String data) {
                    final HomePageBean homePageBean = MyGson.getInstance().fromJson(data, HomePageBean.class);
                    if (homePageBean.getCode().equals("200")) {
                        list_banner = homePageBean.getData().getBanner();
                        list_nav = homePageBean.getData().getNavlist();
                        Log.d("list_nav",list_nav.size()+".");
                        list_HotRecommend = homePageBean.getData().getHotRecommend();
                        list_Promotion = homePageBean.getData().getPromotion();
                        list_Recommend = homePageBean.getData().getRecommend();
                        getBanner(topView);
                        adapter1.setNewData(list_HotRecommend);
                        adapter2.setNewData(list_Promotion);
                        homeRecommendAdapter.setNewData(list_Recommend);
                        adapter = new GridAdapter1(R.layout.item_grid, list_nav);
                        RecycleViewHelp.setGridMannager(recyclerView_nav, 12, 4, getActivity());
                        recyclerView_nav.setAdapter(adapter);
                        getAdvert(homePageBean.getData().getAdvert1(),homePageBean.getData().getAdvert2());
                    } else {
                        ToastUtil.showToast(getActivity(), homePageBean.getMsg());
                    }
                    dialog.dismiss();
                }

                @Override
                public void onError(String msg) {
                    dialog.dismiss();
                    ToastUtil.showToast(getActivity(), msg);
                    super.onError(msg);
                }
            });
        }

    }


    private void getTopView() {
        View topView = LayoutInflater.from(getActivity()).inflate(R.layout.fragment_homepage_top, null);
        homeRecommendAdapter.addHeaderView(topView);
        getleftRightList(topView);
        getPager(topView);
        getNetData(topView);
        recyclerView1 = topView.findViewById(R.id.advert_recycleView1);
        recyclerView2 = topView.findViewById(R.id.advert_recycleView2);
        advert_iv1 = topView.findViewById(R.id.advert_iv1);
        advert_iv2 = topView.findViewById(R.id.advert_iv2);
    }


    private void getAdvert(final List<HomePageBean.Data.Advert1Bean> advert1, final List<HomePageBean.Data.Advert1Bean> advert2) {
        final AdvertAdapter adapter1 = new AdvertAdapter(R.layout.item_advert,advert1);
        RecycleViewHelp.setManager(recyclerView1,getActivity());
        recyclerView1.setAdapter(adapter1);
        GlideUtil.loadImage(getActivity(),NetWorkUtil.URL + advert1.get(0).getImg(),advert_iv1);
        adapter1.setOnRecyclerViewItemClickListener(new BaseQuickAdapter.OnRecyclerViewItemClickListener() {
            @Override
            public void onItemClick(View view, int i) {
                GlideUtil.loadImage(getActivity(),NetWorkUtil.URL + advert1.get(i).getImg(),advert_iv1);
                adapter1.setPos(i);
            }
        });
        final AdvertAdapter adapter2 = new AdvertAdapter(R.layout.item_advert,advert2);
        RecycleViewHelp.setManager(recyclerView2,getActivity());
        recyclerView2.setAdapter(adapter2);
        GlideUtil.loadImage(getActivity(),NetWorkUtil.URL + advert2.get(0).getImg(),advert_iv2);
        adapter2.setOnRecyclerViewItemClickListener(new BaseQuickAdapter.OnRecyclerViewItemClickListener() {
            @Override
            public void onItemClick(View view, int i) {
                GlideUtil.loadImage(getActivity(),NetWorkUtil.URL + advert2.get(i).getImg(),advert_iv2);
                adapter2.setPos(i);
            }
        });
    }

    /**
     * 动态获取viewPager的页数
     */
    private void getPager(View topView) {
        /*viewPager = topView.findViewById(R.id.viewpager);
        List<View> views = new ArrayList<>();
        List<HomeGridBean> data = new ArrayList<>();
        for (int i = 0; i < list_nav.size(); i++) {
            Log.d("XXXX", list_nav.get(i).getName() + "--");
            data.add(new HomeGridBean(list_nav.get(i).getName(), NetWorkUtil.URL + list_nav.get(i).getImg()));
        }
        int pageTotal = data.size() % 8 == 0 ? data.size() / 8 : data.size() / 8 + 1;

        for (int i = 0; i < pageTotal; i++) {
            RecyclerView recyclerView = (RecyclerView) LayoutInflater.from(getActivity()).inflate(R.layout.view_grid, null);
            GridAdapter adapter = new GridAdapter(R.layout.item_grid, data, i, pageTotal);
            RecycleViewHelp.setGridMannager(recyclerView, 12, 4, getActivity());
            recyclerView.setAdapter(adapter);
            views.add(recyclerView);
        }
        GridViewPagerAdapter adapter = new GridViewPagerAdapter(views);
        viewPager.setAdapter(adapter);*/


        recyclerView_nav = topView.findViewById(R.id.recycleView);
//         adapter = new GridAdapter1(R.layout.item_grid, list_nav);
//        RecycleViewHelp.setGridMannager(recyclerView_nav, 12, 4, getActivity());
//        recyclerView_nav.setAdapter(adapter);
    }

    private void getleftRightList(View topView) {
        recycleView_fine_quality = topView.findViewById(R.id.recycleView_fine_quality);
        recycleView_promotion = topView.findViewById(R.id.recycleView_promotion);
        adapter1 = new HomeLeftRightAdapter(R.layout.item_goods, new ArrayList<GoodsBean>());
        adapter2 = new HomeLeftRightAdapter(R.layout.item_goods, new ArrayList<GoodsBean>());
        RecycleViewHelp.setHorizontalManager(recycleView_fine_quality, R.drawable.line_white_3_5, getActivity());
        RecycleViewHelp.setHorizontalManager(recycleView_promotion, R.drawable.line_white_3_5, getActivity());
        recycleView_fine_quality.setAdapter(adapter1);
        recycleView_promotion.setAdapter(adapter2);
    }

    private void getBanner(View topView) {
        bannerLayout = topView.findViewById(R.id.banner);
        List<String> urls = new ArrayList<>();
        if (list_banner.size() > 0) {
            for (int i = 0; i < list_banner.size(); i++) {
                urls.add(NetWorkUtil.URL + list_banner.get(i).getImg());
            }
            bannerLayout.setViewUrls(urls);
            bannerLayout.setOnBannerChangeListener(new BannerLayout.OnBannerChangeListener() {
                @Override
                public void onChange(int position) {

                }
            });
            bannerLayout.setOnBannerItemClickListener(new BannerLayout.OnBannerItemClickListener() {
                @Override
                public void onItemClick(int position) {

                }
            });
        }

    }

    class AdvertAdapter extends BaseQuickAdapter<HomePageBean.Data.Advert1Bean> {
        private int pos = 0;

        public AdvertAdapter(int layoutResId, List<HomePageBean.Data.Advert1Bean> data) {
            super(layoutResId, data);
        }

        @Override
        protected void convert(BaseViewHolder holder, HomePageBean.Data.Advert1Bean bean) {
            TextView textView = holder.getView(R.id.tv);
            if (pos == holder.getAdapterPosition()){
                textView.setTextColor(ContextCompat.getColor(mContext,R.color.red));
            }else {
                textView.setTextColor(ContextCompat.getColor(mContext,R.color.gray6));
            }
            holder.setText(R.id.tv,bean.getName());
        }

        public void setPos(int pos) {
            this.pos = pos;
            notifyDataSetChanged();
        }
    }

}

